/*
ID: icerupt1
PROG: theme
LANG: C++11
*/

/* solution
 *
*/
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>

std::ifstream fin {"theme.in" };
std::ofstream fout{"theme.out"};

int n, ans = 0;

int calc_next(int d, std::vector<int> const & da)
{
	int n = da.size(), max = 0;
	std::vector<int> next; next.resize(n + 2);
	int i = 0, j = next[0] = -1;
	while (i < n) {
		while (j != -1 && da[i] != da[j] + d) j = next[j];
		next[++i] = ++j;
		if (next[i] + n-i-1)
		max = std::max(max, std::min(next[i], i/2));
	}
	return max;
}

int main()
{
	std::vector<int> da;
	fin >> n; da.resize(n + 2);
	for (int i = 0; i < n; i++) fin >> da[i];
	da[++n] = -233;

	for (int first = 0; first < n - 4; first++)
		for (int delta = -87; delta <= 87; delta++) {
			std::vector<int> tmp{da.begin() + first, da.end()};
			ans = std::max(ans, calc_next(delta, tmp));
		}

	if (ans < 5) ans = 0;
	std::cout << ans << '\n';
	fout << ans << '\n';
}

